package com.shanjupay.merchant.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shanjupay.common.exception.BusinessException;
import com.shanjupay.common.exception.CommonErrorCode;
import com.shanjupay.merchant.api.AppService;
import com.shanjupay.merchant.api.dto.AppDTO;
import com.shanjupay.merchant.convert.AppCovert;
import com.shanjupay.merchant.entity.App;
import com.shanjupay.merchant.entity.Merchant;
import com.shanjupay.merchant.mapper.AppMapper;
import com.shanjupay.merchant.mapper.MerchantMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;


/**
 * @projectName: shanjupay
 * @package: com.shanjupay.merchant.service
 * @className: AppServiceImpl
 * @author: Eric
 * @date: 2023/12/8 15:20
 * @version: 1.0
 */
@org.apache.dubbo.config.annotation.Service
public class AppServiceImpl implements AppService {

    @Autowired
    private AppMapper appMapper;

    @Autowired
    private MerchantMapper merchantMapper;

    /**
     * 创建应用程序
     *
     * @param merchantId 商户id
     * @param app        应用程序
     * @return {@link AppDTO}
     * @throws BusinessException 业务异常
     */
    @Override
    public AppDTO createApp(Long merchantId, AppDTO app) throws BusinessException {
        /*
        *   1）校验商户是否通过资质审核
            如果商户资质审核没有通过不允许创建应用。
            2）生成应用ID
            应用Id使用UUID方式生成。
            3）保存商户应用信息
            应用名称需要校验唯一性
        */
        if (merchantId == null || app == null){
            throw new BusinessException(CommonErrorCode.E_300009);
        }
        Merchant merchant = merchantMapper.selectById(merchantId);
        if (merchant == null){
            throw new BusinessException(CommonErrorCode.E_200002);
        }
        if (!merchant.getAuditStatus().equals("2")){
            throw new BusinessException(CommonErrorCode.E_200003);
        }
        if(isExistAppName(app.getAppName())){
            throw new BusinessException(CommonErrorCode.E_200004);
        }
        app.setAppId(UUID.randomUUID().toString());
        app.setMerchantId(merchantId);
        App entity = AppCovert.INSTANCE.dto2entity(app);

        appMapper.insert(entity);

        return AppCovert.INSTANCE.entity2dto(entity);
    }

    @Override
    public List<AppDTO> queryAppByMerchant(Long merchantId) {
        List<App> apps = appMapper.selectList(new QueryWrapper<App>().lambda().eq(App::getMerchantId,merchantId));
        List<AppDTO> appDTOS = AppCovert.INSTANCE.listentity2dto(apps);
        return appDTOS;
    }
    @Override
    public AppDTO getAppById(String id) {
        App app = appMapper.selectOne(new QueryWrapper<App>().lambda().eq(App::getAppId, id));
        return AppCovert.INSTANCE.entity2dto(app);
    }

    /**
     * 是存在应用程序名称
     *
     * @param appName 应用程序名称
     * @return {@link Boolean}
     */
    private Boolean isExistAppName(String appName) {
        Integer count = appMapper.selectCount(new QueryWrapper<App>().lambda().eq(App::getAppName, appName));
        return count.intValue() > 0;
    }
}
